package com.rocker.graduation.mail;

import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

import com.rocker.graduation.util.Config;

public class SenderMailHelpler {
	private transient MailAuthenticator authenticator;
	
	private transient Session session;
	
	private Config config;
	
	private transient Properties prop = System.getProperties();
	
	public SenderMailHelpler(){
		init();
	}
	
	public void init(){
		config = Config.getInstance();
		prop.put("mail.smtp.auth", "false");
		prop.put("mail.smtp.host", "smtp.126.com");
		authenticator = new MailAuthenticator(config.getProperty("username"), config.getProperty("password"));
		session = Session.getInstance(prop, authenticator);
	}
	
	public void send(String recipient, String subject, String content) throws AddressException, MessagingException{
		final MimeMessage message = new MimeMessage(session);
		message.setFrom(new InternetAddress(authenticator.getUsername()));
		message.setRecipient(RecipientType.TO, new InternetAddress(recipient));
		message.setSubject(subject);
		message.setContent(content, "text/html;charset=utf-8");
		Transport.send(message);
	}
}
